# Dockerfile to build Live iso with Anaconda from the repository.
# To find out how to build this container please look on the ./tests/README.rst file.
# This container has to be started as --privileged and with precreated loop devices otherwise
# lorax won't work correctly.
#
# Execution example:
#
# make -f ./Makefile.am container-rpms-scratch # Create Anaconda RPM in `pwd`/result/... directory.
# sudo make -f ./Makefile.am anaconda-live-iso-creator-build
#
# # /var/tmp tmpfs speeds up lorax and avoids https://bugzilla.redhat.com/show_bug.cgi?id=1906364
# sudo podman run -i --rm --privileged --tmpfs /var/tmp:rw,mode=1777 -v `pwd`/result/build/01-rpm-build:/anaconda-rpms:ro -v `pwd`/output-dir:/images:z quay.io/rhinstaller/anaconda-live-iso-creator:main
#
# note:
# - add `--network=slirp4netns` if you need to share network with host computer to reach
#   repositories (VPN for example)
#
{% if distro_name == "rhel" %}
#
# WARNING: this solution is not supported on RHEL/CentOS Stream
#
{% endif %}

# The `image` arg will set base image for the build.
# possible values:
#   registry.fedoraproject.org/fedora:35
#   registry.fedoraproject.org/fedora:rawhide
#   registry-proxy.engineering.redhat.com/rh-osbs/ubi9:latest # private source
#   registry.access.redhat.com/ubi8/ubi # public source
ARG image
FROM ${image}
# FROM starts a new build stage with new ARGs. Put any ARGs after FROM unless required by the FROM itself.
# see https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
LABEL maintainer=anaconda-devel@lists.fedoraproject.org

# Prepare environment and install build dependencies
RUN set -ex; \
  dnf update -y; \
  dnf install -y \
  createrepo_c \
# for clonning cockpit-project/bots/
  git \
# to be able to read ip of the container and pass that to VM
  iproute \
  lorax-lmc-virt; \
  dnf clean all

RUN mkdir /lorax /anaconda-rpms /images

# Generate kickstart file for Fedora Workstation
# TODO: add our local repository to the kickstart file
RUN set -ex; \
  dnf install -y \
# required for ksflatten
  pykickstart; \
  mkdir /fedora-kickstarts; \
{% if distro_release == "rawhide" %}
  git clone -b main https://pagure.io/fedora-kickstarts.git /fedora-kickstarts; \
{% else %}
  git clone -b f{$ distro_release $} https://pagure.io/fedora-kickstarts.git /fedora-kickstarts; \
{% endif %}
  cd /fedora-kickstarts; \
  ksflatten -o /lorax/workstation.ks -c fedora-live-workstation.ks; \
# cleanups of parts which don't need to be part of the container
  dnf remove -y pykickstart; dnf clean all; \
  rm -rf /fedora-kickstarts

COPY ["lmc-build", "/"]

WORKDIR /lorax

ENTRYPOINT /lmc-build
